package com.ferret.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ferret.bean.dto.FaceObject;
import com.ferret.bean.dto.SubImageInfoObject;
import com.ferret.bean.dto.SubImageList;
import com.ferret.bean.vo.FaceInfo;
import com.ferret.config.VIIDDigestAuthenticationEntryPoint;
import com.ferret.service.VIIDServerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
public class TestServerController {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    VIIDServerService viidServerService;
    @Autowired
    VIIDDigestAuthenticationEntryPoint digestAuthenticationEntryPoint;


    /**
     * 布控测试
     */
    @GetMapping("/stranger/bukongData")
    public void bukongData() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOpt("control_id","2083993879116800");
        jsonObject.putOpt("feature_id","2447390485123073");
        jsonObject.putOpt("simi","0.062");
        jsonObject.putOpt("device_id","192.168.15.193");
    }
    /**
     * 通过GET请求获取陌生人的实时数据。
     * 此方法演示了如何生成包含面部信息的JSON数组，并通过MQTT协议发布这些数据。
     */
    @GetMapping("/stranger/realTimeData")
    public void realTimeData() {
        JSONArray jsonArray = new JSONArray();
        FaceInfo faceInfo = new FaceInfo();
        faceInfo.setFaceId(new Date().getTime()+"");
        faceInfo.setDeviceId("192.168.15.193");
        for (int m = 0; m < 2; m++) {
            faceInfo.setShotTime(DateUtil.format(new Date(), "yyyyMMddHHmmss"));
            faceInfo.setStoragePath("http://192.168.15.89:9999/face/2024/05/29/192.168.15.193/20240529143754353_face.jpg");
            jsonArray.add(faceInfo);
        }

        for (int m = 0; m < 2; m++) {
            faceInfo.setShotTime(DateUtil.format(new Date(), "yyyyMMddHHmmss"));
            faceInfo.setStoragePath("http://192.168.15.89:9999/face/2024/05/29/192.168.15.193/20240529144623386_face.jpg");
            jsonArray.add(faceInfo);
        }

        String resArr = JSONUtil.toJsonStr(jsonArray);
    }


    /**
     *
     * 发送陌生人订阅
     * @param servletRequest
     * @return
     */
    @GetMapping("/stranger/sendStranger")
    public void vSubscribes(@RequestParam String featureid,HttpServletRequest servletRequest) {
        JSONObject jsonObject = JSONUtil.createObj();
        jsonObject.putOpt("featureid", featureid);
        jsonObject.putOpt("force_push", "11");
    }

    public String readFile() {
        List<String> ss = FileUtil.readLines(new File("C:\\Users\\Administrator\\Documents\\WeChat Files\\wxid_l6p627kjpoh822\\FileStorage\\File\\2024-03\\2024-03-22.txt"), "utf-8");
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < ss.size(); i++) {
            JSONObject jsonObject = JSONUtil.parseObj(ss.get(i));
            JSONArray faceObjectList = (JSONArray) jsonObject.get("FaceObject");
            for (int j = 0; j < faceObjectList.size(); j++) {
                FaceObject faceObject = Convert.convert(FaceObject.class, faceObjectList.get(j));
                JSONObject resObj = new JSONObject();
                resObj.putOpt("faceId", faceObject.getFaceID());
                resObj.putOpt("deviceId", "192.168.15.193");
                resObj.putOpt("cameraid","192.168.15.193");
                SubImageList subImageList = faceObject.getSubImageList();
                List<SubImageInfoObject> subImageInfoObjects = subImageList.getSubImageInfoObject();
                for (int m = 0; m < subImageInfoObjects.size(); m++) {
                    SubImageInfoObject subImageInfoObject = subImageInfoObjects.get(m);
                    if (StrUtil.equals(subImageInfoObject.getType(), "11")) {
                        resObj.putOpt("shotTime", subImageInfoObject.getShotTime());
                        resObj.putOpt("storagePath", subImageInfoObject.getStoragePath());
                    }
                }
                jsonArray.add(resObj);
            }
        }
        return JSONUtil.toJsonStr(jsonArray);
    }

}
